package com.zhentao.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.time.LocalDateTime;

/**
 * AI会话实体类
 * 用于管理用户与AI的对话会话
 */
@Document(collection = "ai_conversations")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AiConversation {
    
    @Id
    private String id;
    
    /**
     * 用户ID - 会话所属用户
     */
    @Indexed
    @Field("userId")
    private Integer userId;
    
    /**
     * 会话ID - 用于Redis存储的唯一标识
     */
    @Indexed
    @Field("conversationId")
    private String conversationId;
    
    /**
     * 会话标题
     */
    @Field("title")
    private String title;
    
    /**
     * 会话描述/摘要
     */
    @Field("description")
    private String description;
    
    /**
     * 创建时间
     */
    @Indexed
    @Field("createTime")
    private LocalDateTime createTime;
    
    /**
     * 最后更新时间
     */
    @Indexed
    @Field("updateTime")
    private LocalDateTime updateTime;
    
    /**
     * 最后一条消息时间
     */
    @Indexed
    @Field("lastMessageTime")
    private LocalDateTime lastMessageTime;
    
    /**
     * 最后一条消息内容（用于预览）
     */
    @Field("lastMessage")
    private String lastMessage;
    
    /**
     * 消息总数
     */
    @Field("messageCount")
    private Integer messageCount = 0;
    
    /**
     * 会话状态
     * ACTIVE: 活跃
     * ARCHIVED: 已归档
     * DELETED: 已删除
     */
    @Indexed
    @Field("status")
    private String status = "ACTIVE";
    
    /**
     * 是否置顶
     */
    @Field("pinned")
    private Boolean pinned = false;
    
    /**
     * 会话标签（可用于分类）
     */
    @Field("tags")
    private String tags;
    
    /**
     * 扩展字段（JSON格式）
     */
    @Field("metadata")
    private String metadata;
    
    /**
     * 构造函数 - 创建新会话
     */
    public AiConversation(Integer userId, String conversationId, String title) {
        this.userId = userId;
        this.conversationId = conversationId;
        this.title = title;
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.status = "ACTIVE";
        this.messageCount = 0;
        this.pinned = false;
    }
    
    /**
     * 更新最后消息信息
     */
    public void updateLastMessage(String message) {
        this.lastMessage = message;
        this.lastMessageTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.messageCount = (this.messageCount == null ? 0 : this.messageCount) + 1;
    }
    
    /**
     * 归档会话
     */
    public void archive() {
        this.status = "ARCHIVED";
        this.updateTime = LocalDateTime.now();
    }
    
    /**
     * 删除会话
     */
    public void delete() {
        this.status = "DELETED";
        this.updateTime = LocalDateTime.now();
    }
    
    /**
     * 恢复会话
     */
    public void restore() {
        this.status = "ACTIVE";
        this.updateTime = LocalDateTime.now();
    }
    
    /**
     * 置顶/取消置顶
     */
    public void togglePin() {
        this.pinned = !this.pinned;
        this.updateTime = LocalDateTime.now();
    }
}
